package com.gss.armory.web;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.gss.armory.domain.DocumentCategoryDTO;
import com.gss.armory.domain.DocumentSubCategory;
import com.gss.armory.persistence.CommonDAO;
import com.gss.armory.service.CommonApplicationService;
import com.gss.armory.service.DocumentMetaDataService;
import com.gss.common.util.ApplicationMaps;
import com.gss.common.util.ApplicationUtilities;

@Controller
public class CommonUtilityController {
	
	private Logger logger = Logger.getLogger(CommonUtilityController.class);
	
	@Autowired
	DocumentMetaDataService documentMetaDataService;
	
	@Autowired
	CommonApplicationService commonApplicationService;
	
	@Autowired
	CommonDAO commonDAO;
	
/*	@RequestMapping(value="/loadAttributes.htm")
	public ModelAndView loadAttributes(@RequestParam("documentType")String documentType,HttpServletResponse response)
	{
		ModelAndView mav = new ModelAndView("docUpload/metaDataAttributesFill");
		DocDetailsForm documentUploadForm = new DocDetailsForm();
		logger.debug("loading attributes");
		List<DocumentMetaDataAttributesDTO> attributesDTOs =documentMetaDataService.getDataAttributesDTOs(Integer.parseInt(documentType));
		List<DocMetaDataAttrForm> attrValueList = new ArrayList<DocMetaDataAttrForm>(attributesDTOs.size());
		documentUploadForm.setMetaDataAttrList(attrValueList);
		mav.addObject("docAttributesList",attributesDTOs);
		mav.addObject("docDetailsForm", documentUploadForm);
		return mav;
	}*/
	
	@RequestMapping(value="/fetchCategory.htm")
	public void fetchDown(@RequestParam("parentlevel")String parentlevel,HttpServletResponse response)
	{
		List<DocumentCategoryDTO> categoryDTOs;
		if(Integer.parseInt(parentlevel) != 0){
			categoryDTOs = documentMetaDataService.getCategoryFrmType(Integer.parseInt(parentlevel));
		}
		else{
			categoryDTOs = commonDAO.loadAll(DocumentCategoryDTO.class);
		}
		JSONArray array = new JSONArray();
		if(null != categoryDTOs){
			for (DocumentCategoryDTO documentCategoryDTO : categoryDTOs) {
				HashMap<String, String> hashMap = new HashMap<String, String>();
				hashMap.put("optionValue", documentCategoryDTO.getDcId().toString());
				hashMap.put("optionDisplay", documentCategoryDTO.getDcName());
				array.put(hashMap);
			}
		}
		try{
			response.getWriter().write(array.toString());
		}
		catch (IOException e) {
			//e.printStackTrace();
			logger.error(e.getMessage());
		}
	}
	
	@RequestMapping(value="/fetchSubCategory.htm")
	public void fetchDownSubCat(@RequestParam("parentlevel")String parentlevel,HttpServletResponse response)
	{
		List<DocumentSubCategory> subCategoryDTOs = null;
		if(!ApplicationUtilities.checkIfNullOrBlank(parentlevel))
			subCategoryDTOs =documentMetaDataService.getSubCategoryFrmCat(Integer.parseInt(parentlevel));
		JSONArray array = new JSONArray();
		if(null != subCategoryDTOs){
			for (DocumentSubCategory documentSubCategoryDTO : subCategoryDTOs) {
				HashMap<String, String> hashMap = new HashMap<String, String>();
				hashMap.put("optionValue", documentSubCategoryDTO.getDscId().toString());
				hashMap.put("optionDisplay", documentSubCategoryDTO.getDscName());
				array.put(hashMap);
			}
		}
		try{
			response.getWriter().write(array.toString());
		}
		catch (IOException e) {
			//e.printStackTrace();
			logger.error(e.getMessage());
		}
	}
	
	@RequestMapping(value="/getDepttForBU.htm")
	public void fetchDepttForBU(@RequestParam("buId") int buId , HttpServletResponse response){
		JSONArray array = new JSONArray();
		HashMap<Long, String> depttIdNameMap = new HashMap<Long, String>();
		if(buId == 0){
			depttIdNameMap = ApplicationMaps.DEPTID_DEPTSNAME_MAP;
			
		}
		else
		{
			depttIdNameMap = commonApplicationService.getDepttIdNameMapForBUId(buId);
		}
		for(Long depttId : depttIdNameMap.keySet()){
			HashMap<String, String> hashMap = new HashMap<String, String>();
			hashMap.put("optionValue", depttId.toString() );
			hashMap.put("optionDisplay", depttIdNameMap.get(depttId));
			array.put(hashMap);
		}
		try{
			response.getWriter().write(array.toString());
		}
		catch (IOException e) {
			//e.printStackTrace();
			logger.error(e.getMessage());
		}
	}
	
	@RequestMapping(value="/getLocForCompany.htm")
	public void fetchBusinessUnitForComp(@RequestParam("compId") int cdId, HttpServletResponse response){
		JSONArray array = new JSONArray();
		
		HashMap<Long, String> locIdNameMap = new HashMap<Long, String>();
		if(cdId == 0){
			locIdNameMap = ApplicationMaps.BUID_BUSNAME_MAP;
			locIdNameMap.put((long)0, "Choose Location");
		}
		else{
		locIdNameMap = commonApplicationService.getLocforCompany(cdId);
		}
			
		for(Long locId : locIdNameMap.keySet()){
			HashMap<String, String> hashMap = new HashMap<String, String>();
			hashMap.put("optionValue", locId.toString());
			hashMap.put("optionDisplay", locIdNameMap.get(locId));
			array.put(hashMap);
		}
		
		logger.debug(locIdNameMap);
		logger.debug(array.toString());
		
		try{
			response.getWriter().write(array.toString());
		}
		catch(IOException e){
			//e.printStackTrace();
			logger.error(e.getMessage());
		}
	}
}
